package fonkin.com.wifisignapp.widget;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;



import java.util.ArrayList;



/**
 * Title :
 * Description :
 * Author : Jerry xu    date : 2017/2/12  15:23
 * Update :             date :
 * Version : 1.0.0
 */
public abstract class MyRecyclerViewAdapter<VH extends RecyclerView.ViewHolder>
        extends RecyclerView.Adapter<VH> /*implements MyRecyclerViewHeaderInter<RecyclerView.ViewHolder>*/ {

    /**
     * Returns the number of items in the adapter bound to the parent
     * RecyclerView.
     *
     * @return The number of items in the bound adapter
     */
//	public abstract int getAdapterItemCount();

    public ArrayList<Object> itemList = new ArrayList<>();
    protected View customLoadMoreView = null;
    protected MyRecyclerView.CustomRelativeWrapper customHeaderView = null;
    private View tempCustomLoadMoreView = null;
    private ProgressBar pb;
    private TextView tv;
    private OnItemClickListener listener;

    public MyRecyclerViewAdapter() {
        super();
    }

    @Override
    public VH onCreateViewHolder(ViewGroup parent, int viewType) {
        if (/*footerType==bottomFooter && */viewType == VIEW_TYPES.FOOTER) {
            VH viewHolder = getViewHolder(customLoadMoreView);
            if (getAdapterItemCount() == 0)
                viewHolder.itemView.setVisibility(View.GONE);
            return viewHolder;
        } else if (viewType == VIEW_TYPES.HEADER) {
            if (customHeaderView != null)
                return getViewHolder(customHeaderView);
        }

        return onCreateViewHolder(parent);
    }

    public void removeCustomLoadMoreView() {
        if (customLoadMoreView != null) {
            tempCustomLoadMoreView = customLoadMoreView;
        }
        customLoadMoreView = null;
        notifyDataSetChanged();
    }

    public void reEnableCustomLoadMoreView() {
        if (customLoadMoreView == null && tempCustomLoadMoreView != null) {
            customLoadMoreView = tempCustomLoadMoreView;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == (getItemCount() - 1) && customLoadMoreView != null) {
            return VIEW_TYPES.FOOTER;
        } else if (position == 0 && customHeaderView != null) {
            return VIEW_TYPES.HEADER;
        } else {
            return VIEW_TYPES.NORMAL;
        }
    }

    /**
     * Returns the total number of items in the data set hold by the adapter.
     *
     * @return The total number of items in this adapter.
     */
    @Override
    public int getItemCount() {
        int headerOrFooter = 0;
        if (customHeaderView != null) headerOrFooter++;
        if (customLoadMoreView != null) headerOrFooter++;
        return getAdapterItemCount() + headerOrFooter;
    }

    public abstract VH getViewHolder(View view);

    public abstract VH onCreateViewHolder(ViewGroup parent);

    public abstract void onBindNormalViewHolder(VH holder, int position);

    public void addOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onBindViewHolder(VH holder, int position) {
        if (position < getItemCount() && (customHeaderView != null ? position <= getAdapterItemCount() : position < getAdapterItemCount()) && (customHeaderView != null ? position > 0 : true)) {
            position = customHeaderView != null ? position - 1 : position;

            if (listener != null) {
                holder.itemView.setOnClickListener(new MyOnClickListener(position));
                holder.itemView.setOnLongClickListener(new MyOnLongClickListener(position));
            }

            onBindNormalViewHolder(holder, position);
        }

    }

    public View getCustomLoadMoreView() {
        return customLoadMoreView;
    }

    /**
     * Using a custom LoadMoreView
     *
     * @param customView the inflated view
     */
    public void setCustomLoadMoreView(View customView) {
        customLoadMoreView = customView;
    }



    public void isHasMoreData(boolean flag) {
        if (pb != null && tv != null) {
            if (flag) {
                pb.setVisibility(View.VISIBLE);
                tv.setVisibility(View.GONE);
            } else {
                pb.setVisibility(View.GONE);
                tv.setVisibility(View.VISIBLE);
            }
        }
    }

    public MyRecyclerView.CustomRelativeWrapper getCustomHeaderView() {
        return customHeaderView;
    }

    /**
     * Set the header view of the adapter.
     */
    public void setCustomHeaderView(MyRecyclerView.CustomRelativeWrapper customHeaderView) {
        this.customHeaderView = customHeaderView;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            final GridLayoutManager gridLayoutManager = ((GridLayoutManager) layoutManager);
            //确保头部在顶部而不是gridView的左边
            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {

                @Override
                public int getSpanSize(int position) {
                    //头部的角标为0 那么让它横跨二列 其他的正常就是一列
                    //尾部角标为最大 那么让它横跨二列 其他的正常就是一列
                    if (customHeaderView != null && position == 0) {
                        return gridLayoutManager.getSpanCount();
                    } else if (customHeaderView == null ? position == getAdapterItemCount() : position == getAdapterItemCount() + 1) {
                        return gridLayoutManager.getSpanCount();
                    } else {
                        return 1;
                    }
                }
            });
        }
    }

    @Override
    public void onViewAttachedToWindow(VH holder) {
        super.onViewAttachedToWindow(holder);
        if (isStaggeredGridLayout(holder)) {
            handleLayoutIfStaggeredGridLayout(holder, holder.getLayoutPosition());
        }
    }

    private boolean isStaggeredGridLayout(RecyclerView.ViewHolder holder) {
        ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams();
        if (layoutParams != null && layoutParams instanceof StaggeredGridLayoutManager.LayoutParams) {
            return true;
        }
        return false;
    }

    protected void handleLayoutIfStaggeredGridLayout(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == VIEW_TYPES.FOOTER || getItemViewType(position) == VIEW_TYPES.HEADER) {
            StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
            layoutParams.setFullSpan(true);
        }

    }

    public <T> void addItem(ArrayList<T> itemList) {
        this.itemList.addAll(itemList);
    }

    public <T> void addItem(T t) {
        this.itemList.add(t);
    }

    public int getAdapterItemCount() {
        if (itemList == null || itemList.size() == 0) {
            return 0;
        } else {
            return itemList.size();
        }
    }

    public void clear() {
        itemList.clear();
    }

    protected class VIEW_TYPES {
        public static final int NORMAL = 0;
        public static final int HEADER = 1;
        public static final int FOOTER = 2;
    }

    private class MyOnClickListener implements OnClickListener {
        private int position;

        public MyOnClickListener(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            listener.onItemClick(v, position);
        }

    }

    private class MyOnLongClickListener implements OnLongClickListener {
        private int position;

        public MyOnLongClickListener(int position) {
            this.position = position;
        }

        @Override
        public boolean onLongClick(View v) {
            listener.onItemLongClick(v, position);
            return false;
        }

    }

}
